<?php

namespace app\admin\controller;

use app\common\BaseHelper;
use app\admin\model\Basicdata;
use app\admin\model\Member;
use app\admin\model\Povertymember;
use app\admin\model\Povertyparty;
use app\admin\model\Area;
use app\admin\model\Villages;

use think\Request;
use think\Controller;

class Layer extends Controller
{

    /**
     * 获取所有村民
     */
    public function memberList(){
        $request = Request::instance();
        $param = $request->param();

        $start = $param['start'];  //每页起始条数
        $length = $param['length'];  //每页条数

        $where = [
            'isDelete' => 2
        ];
        $search = json_decode($param['search']['value'],true);
        if(isset($search['name']) && $search['name']){  //搜索
            $where['name'] = ['like','%'.$search['name'].'%'];
        }
        if(isset($param['villageId']) && $param['villageId']) {
            $where['villageId'] = $param['villageId'];
        }

        $list = Member::where($where)->field('id,name,avatar,gender,birthday,identityNumber,mobile')->limit($start,$length)->select();
        $count = Member::where($where)->count();
        $res = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
            'test' => $param
        ];

        return json($res);
    }

    //帮扶人列表-去除贫困户-服务器端分页
    public function memberAidList() {
        $request = Request::instance();
        $param = $request->param();

        $start = $param['start'];  //每页起始条数
        $length = $param['length'];  //每页条数

        $where = [
            'villageId' => session('ADMIN')['villageId'],
            'isDelete' => 2
        ];
        $search = json_decode($param['search']['value'],true);
        if($search['name']){  //搜索
            $where['name'] = ['like','%'.$search['name'].'%'];
        }

        $poverty = Povertymember::where('villageId',session('ADMIN')['villageId'])->select();
        $povertyIds = [];
        foreach ($poverty as $v) {
            $povertyIds[] = $v['memberId'];
        }
        $where['id'] = ['not in',implode(',', $povertyIds)];

        $list = Member::where($where)->field('id,name,avatar,gender,birthday,identityNumber,mobile')->limit($start,$length)->select();
        $count = Member::where($where)->count();
        $res = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count
        ];

        return json($res);
    }

    //帮扶人列表-去除贫困户-客户端分页
    public function memberAidList2() {
        $request = Request::instance();
        $param = $request->param();

        $where = [
            'isDelete' => 2
        ];

        $poverty = Povertymember::where('villageId',session('ADMIN')['villageId'])->select();
        $povertyIds = [];
        foreach ($poverty as $v) {
            $povertyIds[] = $v['memberId'];
        }
        $where['id'] = ['not in',implode(',', $povertyIds)];

        $list = Member::where($where)->field('*')->select();
        $count = Member::where($where)->count();
        $res = [
            'status' => '1',
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count
        ];

        return json($res);
    }

    //所有贫困户-服务器分页
    public function povertymember() {
        $request = Request::instance();
        $param = $request->param();

        $start = $param['start'];  //每页起始条数
        $length = $param['length'];  //每页条数

        $where = [];
        $search = json_decode($param['search']['value'],true);
        if($search['name']){  //搜索
            $where['name'] = ['like','%'.$search['name'].'%'];
        }

        if(isset($param['villageId']) && $param['villageId']) {
            $where['a.villageId'] = $param['villageId'];
        }
        $where['b.isDelete'] = 2;
        $list = Povertymember::alias('a')->where($where)->join('member b','a.memberId=b.id')->field('b.id,b.name,b.gender,b.birthday,b.identityNumber,a.aidingId')->limit($start,$length)->select();
        //$count = Povertymember::where(['villageId'=>session('ADMIN')['villageId']])->count();
        $count = Povertymember::alias('a')->where($where)->join('member b','a.memberId=b.id')->count();
        $res = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count
        ];

        return json($res);
    }

    //所有贫困户-客户端分页
    public function povertymember2() {
        $request = Request::instance();
        $param = $request->param();

        $where = [];
        if(isset($param['villageId']) && $param['villageId']) {
            $where['a.villageId'] = $param['villageId'];
        }
        if(isset($param['townId']) && $param['townId']) {
            $where['a.townId'] = $param['townId'];
        }
        if(isset($param['xianId']) && $param['xianId']) {
            $where['a.xianId'] = $param['xianId'];
        }
        if(isset($param['cityId']) && $param['cityId']) {
            $where['a.cityId'] = $param['cityId'];
        }
        $where['a.isPoverty'] = 1;
        $list = Member::alias('a')->where($where)->join('povertymember b','a.id=b.memberId')->field('a.id,a.name,a.gender,a.birthday,a.identityNumber,b.aidingId')->select();
        //$count = Povertymember::where(['villageId'=>session('ADMIN')['villageId']])->count();
        $count = Member::alias('a')->where($where)->join('povertymember b','a.id=b.memberId')->count();
        $res = [
            'status' => '1',
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
            'test' => $param
        ];

        return json($res);
    }

    /**
     * 获取所有党员
     */
    public function partyList() {
        $request = Request::instance();
        $param = $request->param();

        $start = $param['start'];  //每页起始条数
        $length = $param['length'];  //每页条数

        $where = [
            'a.isDelete' => 2,
            'a.isParty' => 1,
            'a.checkParty' => ['<', 2]
        ];
        if(isset($param['townId']) && $param['townId']) {
            $where['a.townId'] = $param['townId'];
        }
        if(isset($param['villageId']) && $param['villageId']) {
            $where['a.villageId'] = $param['villageId'];
        }
        if(isset($param['memberId']) && $param['memberId']) {
            $where['a.id'] = ['neq', $param['memberId']];
        }

        // 搜索
        $search = json_decode($param['search']['value'],true);
        if($search['name']){
            $where['a.name'] = ['like','%'.$search['name'].'%'];
        }

        $list = Member::alias('a')->where($where)->join('organization b','a.organizationId=b.id')->field('a.id,a.name,a.gender,a.job,b.name OrgName')->limit($start,$length)->select();
        $count = Member::alias('a')->where($where)->join('organization b','a.organizationId=b.id')->field('a.id,a.name,a.gender,a.job,b.name OrgName')->count();
        $res = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count
        ];

        return json($res);
    }

    /**
     * 获取所有户主
     */
    public function householderList() {
        $request = Request::instance();
        $param = $request->param();

        $start = $param['start'];  //每页起始条数
        $length = $param['length'];  //每页条数

        $where = [
            'a.isDelete' => 2,
            'a.checkVillager' => ['<', 2],
            'b.isHouseholder' => 1
        ];
        if(isset($param['townId']) && $param['townId']) {
            $where['a.townId'] = $param['townId'];
        }
        if(isset($param['villageId']) && $param['villageId']) {
            $where['a.villageId'] = $param['villageId'];
        }
        if(isset($param['memberId']) && $param['memberId']) {
            $where['a.id'] = ['neq', $param['memberId']];
        }

        // 搜索
        $search = json_decode($param['search']['value'],true);
        if($search['name']){
            $where['a.name'] = ['like','%'.$search['name'].'%'];
        }

        $list = Member::alias('a')->where($where)->join('memberextrainfo b','a.id=b.memberId')->field('a.id, a.memberType, a.townId, a.villageId, a.name, a.mobile')->limit($start,$length)->select();
        $count = Member::alias('a')->where($where)->join('memberextrainfo b','a.id=b.memberId')->count();

        if ($list) {
            foreach ($list as $item) {
                if ($item['memberType'] == 2) {
                    $item['village'] = '外来人口';
                } elseif ($item['memberType'] == 3) {
                    $item['village'] = '流动人口';
                } else {
                    if ($item['villageId']) {
                        $item['village'] = Villages::where('id', $item['villageId'])->value('name');
                    } else {
                        $item['village'] = Area::where('id', $item['townId'])->value('name');
                    }
                }
            }
        }

        $res = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count
        ];

        return json($res);
    }

    /**
     * 村民姓名模糊查询
     */
    public function ajaxMembers() {
        $request = Request::instance();
        $param = $request->param();

        $keyword = $param['keyword'];
        $where = [
            'isDelete' => 2
        ];
        if($keyword){
            $where['name'] = ['like', '%' . $keyword . '%'];
        }
        if(isset($param['except'])){
            // TODO 不包含的数据
        }

        $result = Member::field('id,name,avatar,gender,birthday,identityNumber,mobile')->where($where)->select();
        $count = Member::where($where)->count();
        $res = [
            'total_count' => $count,
            'items' => $result,
        ];

        return json($res);
    }

    /**
     * 获取县、镇数据
     */
    public function getCounty() {
        $request = Request::instance();
        $param = $request->param();
        $pid = $param['id'];

        $areaMdl = new Area();
        // $list = Area::where(['parentId'=>$pid,'level'=>$level])->select();
        $list = BaseHelper::makeOptions(
            $areaMdl,
            ['parentId' => $pid],
            ['id' => 'asc']
        );
        return json($list);
    }

    /**
     * 获取 分类/作物/品种
     */
    public function getCropCategory(){
        $params = $this->request->param();
        $parentId = $params['p_id'];
        $model = new Basicdata();

        $html = '';
        if($parentId !== ''){
            $data = $model->where(['type' => 'crop', 'parentId' => $parentId])->field('id, name')->select();

            if($data){
                foreach($data as $item){
                    $html .= "<option value='{$item['id']}'>{$item['name']}</option>";
                }
            }
        }

        return json($html);
    }

    /**
     * 根据镇id获取村列表
     */
    public function getVillage() {
        $request = Request::instance();
        $param = $request->param();
        $pid = $param['id'];

        $villageMdl = new Villages();
        $list = BaseHelper::makeOptions(
            $villageMdl,
            ['townId' => $pid],
            ['id' => 'asc']
        );
        return json($list);
    }

    /**
     * 获取组织
     */
    public function getOrganization() {
        $request = Request::instance();
        $param = $request->param();
        $where = [];
        if(isset($param['villageId']) && $param['villageId']) {
            $where['villageId'] = $param['villageId'];
        }
        if(isset($param['townId']) && $param['townId']) {
            $where['townId'] = $param['townId'];
        }
        if(isset($param['xianId']) && $param['xianId']) {
            $where['xianId'] = $param['xianId'];
        }
        if(isset($param['cityId']) && $param['cityId']) {
            $where['cityId'] = $param['cityId'];
        }
        $list = \app\admin\model\Organization::where(['isDelete'=>2])->where($where)->select();
        $return = [];
        foreach ($list as $v) {
            $return[$v['id']] = $v['name'];
        }
        return json($return);
    }


    // 获取文章分类
    public function getArticleType() {
        $request = Request::instance();
        $param = $request->param();

        // 每页起始条数
        $start = $param['start'];
        // 每页显示条数
        $length = $param['length'];

        $where = [
            'type' => $param['typeId']
        ];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);
            if(isset($filter['name']) && $filter['name']){
                $where['name'] = ['like', '%' . $filter['name'] . '%'];
            }
        }

        $model = model('Articletype');
        $list = $model->where($where)->limit($start, $length)->select();
        $count = $model->where($where)->count();

        $result = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
        ];

        return json($result);
    }

    // 获取文章列表
    public function getArticleList() {
        $request = Request::instance();
        $param = $request->param();

        // 每页起始条数
        $start = $param['start'];
        // 每页显示条数
        $length = $param['length'];
        // 排序条件
        $columns = $param['order'][0]['column'];
        $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];

        $where = [
            'type' => $param['typeId']
        ];
        if(isset($param['townId'])){
            $where['townId'] = $param['townId'];
        }
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);
            if(isset($filter['cityId']) && $filter['cityId']){
                $where['cityId'] = $filter['cityId'];
            }
            if(isset($filter['xianId']) && $filter['xianId']){
                $where['xianId'] = $filter['xianId'];
            }
            if(isset($filter['townId']) && $filter['townId']){
                $where['townId'] = $filter['townId'];
            }
            if(isset($filter['villageId']) && $filter['villageId']){
                $where['villageId'] = $filter['villageId'];
            }

            if(isset($filter['title']) && $filter['title']){
                $where['title'] = ['like', '%'.$filter['title'].'%'];
            }
        }

        $model = model('Article');
        $list = $model->where($where)->limit($start, $length)->order($order)->select();
        $count = $model->where($where)->count();

        $result = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
        ];

        return json($result);
    }

    // 获取村子列表
    public function getVillageList() {
        $request = Request::instance();
        $param = $request->param();

        // 每页起始条数
        $start = $param['start'];
        // 每页显示条数
        $length = $param['length'];
        // 排序条件
        $columns = $param['order'][0]['column'];
        $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];

        $where = [];
        if(isset($param['townId'])){
            $where['townId'] = $param['townId'];
        }
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);
            if(isset($filter['cityId']) && $filter['cityId']){
                $where['cityId'] = $filter['cityId'];
            }
            if(isset($filter['xianId']) && $filter['xianId']){
                $where['xianId'] = $filter['xianId'];
            }
            if(isset($filter['townId']) && $filter['townId']){
                $where['townId'] = $filter['townId'];
            }
            if(isset($filter['villageId']) && $filter['villageId']){
                $where['villageId'] = $filter['villageId'];
            }

            if(isset($filter['name']) && $filter['name']){
                $where['name'] = ['like', '%'.$filter['name'].'%'];
            }
        }

        $model = model('Villages');
        $list = $model->where($where)->limit($start, $length)->order($order)->select();
        $count = $model->where($where)->count();

        $result = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
        ];

        return json($result);
    }

    // 获取农事分类
    public function getKnowledgeType() {
        $request = Request::instance();
        $param = $request->param();

        // 每页起始条数
        $start = $param['start'];
        // 每页显示条数
        $length = $param['length'];
        // 排序条件
        $columns = $param['order'][0]['column'];
        $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];

        $where = [];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);
            $where['name'] = ['like', '%' . $filter['name'] . '%'];
        }

        $model = model('Knowledgetype');
        $list = $model->where($where)->limit($start, $length)->order($order)->select();
        $count = $model->where($where)->count();

        $result = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
        ];

        return json($result);
    }

    // 获取农事知识
    public function getKnowledgeList() {
        $request = Request::instance();
        $param = $request->param();

        // 每页起始条数
        $start = $param['start'];
        // 每页显示条数
        $length = $param['length'];
        // 排序条件
        $columns = $param['order'][0]['column'];
        $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];

        $where = [];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);
            if(isset($filter['typeId']) && $filter['typeId']){
                $where['typeId'] = $filter['typeId'];
            }

            if(isset($filter['title']) && $filter['title']){
                $where['title'] = ['like', '%'.$filter['title'].'%'];
            }
        }

        $model = model('Knowledge');
        $list = $model->where($where)->limit($start, $length)->order($order)->select();
        $count = $model->where($where)->count();

        $result = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
        ];

        return json($result);
    }

    // 获取特色专题列表
    public function getFeaturecolumnList() {
        $request = Request::instance();
        $param = $request->param();

        // 每页起始条数
        $start = $param['start'];
        // 每页显示条数
        $length = $param['length'];
        // 排序条件
        $columns = $param['order'][0]['column'];
        $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];

        $where = [];
        if(isset($param['townId'])){
            $where['townId'] = $param['townId'];
        }
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);
            if(isset($filter['cityId']) && $filter['cityId']){
                $where['cityId'] = $filter['cityId'];
            }
            if(isset($filter['xianId']) && $filter['xianId']){
                $where['xianId'] = $filter['xianId'];
            }
            if(isset($filter['townId']) && $filter['townId']){
                $where['townId'] = $filter['townId'];
            }
            if(isset($filter['villageId']) && $filter['villageId']){
                $where['villageId'] = $filter['villageId'];
            }

            if(isset($filter['name']) && $filter['name']){
                $where['name'] = ['like', '%'.$filter['name'].'%'];
            }
        }

        $model = model('Featurecolumn');
        $list = $model->where($where)->limit($start, $length)->order($order)->select();
        $count = $model->where($where)->count();

        $result = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
        ];

        return json($result);
    }

    // 获取特色内容列表
    public function getFeaturesList() {
        $request = Request::instance();
        $param = $request->param();

        // 每页起始条数
        $start = $param['start'];
        // 每页显示条数
        $length = $param['length'];
        // 排序条件
        $columns = $param['order'][0]['column'];
        $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];

        $where = [];
        if(isset($param['townId'])){
            $where['townId'] = $param['townId'];
        }
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);
            if(isset($filter['cityId']) && $filter['cityId']){
                $where['cityId'] = $filter['cityId'];
            }
            if(isset($filter['xianId']) && $filter['xianId']){
                $where['xianId'] = $filter['xianId'];
            }
            if(isset($filter['townId']) && $filter['townId']){
                $where['townId'] = $filter['townId'];
            }
            if(isset($filter['villageId']) && $filter['villageId']){
                $where['villageId'] = $filter['villageId'];
            }

            if(isset($filter['typeId']) && $filter['typeId']){
                $where['typeId'] = $filter['typeId'];
            }
            if(isset($filter['title']) && $filter['title']){
                $where['title'] = ['like', '%'.$filter['title'].'%'];
            }
        }

        $model = model('Features');
        $list = $model->where($where)->limit($start, $length)->order($order)->select();
        $count = $model->where($where)->count();

        $result = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
        ];

        return json($result);
    }

    // 获取微课堂分类
    public function getMicroclassroomType() {
        $request = Request::instance();
        $param = $request->param();

        $category = get_microclass_category();
        $list = [];
        foreach ($category as $key => $value) {
            $item = [
                'id' => $key,
                'name' => $value
            ];
            $list[] = $item;
        }
        $count = count($list);

        $result = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
        ];

        return json($result);
    }

    // 获取产品分类
    public function getProductType() {
        $request = Request::instance();
        $param = $request->param();

        // 每页起始条数
        $start = $param['start'];
        // 每页显示条数
        $length = $param['length'];

        $where = [];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);
            if(isset($filter['name']) && $filter['name']){
                $where['name'] = ['like', '%'.$filter['name'].'%'];
            }
        }

        $model = model('Producttype');
        $list = $model->where($where)->limit($start, $length)->select();
        $count = $model->where($where)->count();

        $result = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
        ];

        return json($result);
    }

    // 获取产品列表
    public function getProductList() {
        $request = Request::instance();
        $param = $request->param();

        // 每页起始条数
        $start = $param['start'];
        // 每页显示条数
        $length = $param['length'];
        // 排序条件
        $columns = $param['order'][0]['column'];
        $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];

        $where = [
            'style' => $param['style']
        ];
        if(isset($param['townId'])){
            $where['townId'] = $param['townId'];
        }
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);
            if(isset($filter['cityId']) && $filter['cityId']){
                $where['cityId'] = $filter['cityId'];
            }
            if(isset($filter['xianId']) && $filter['xianId']){
                $where['xianId'] = $filter['xianId'];
            }
            if(isset($filter['townId']) && $filter['townId']){
                $where['townId'] = $filter['townId'];
            }
            if(isset($filter['villageId']) && $filter['villageId']){
                $where['villageId'] = $filter['villageId'];
            }

            if(isset($filter['title']) && $filter['title']){
                $where['title'] = ['like', '%'.$filter['title'].'%'];
            }
        }

        $model = model('Product');
        $list = $model->where($where)->limit($start, $length)->order($order)->select();
        $count = $model->where($where)->count();

        foreach ($list as $item) {
            $item['townName'] = Area::where(['id' => $item['townId']])->value('name');
            if($item['villageId']){
                $item['villageName'] = Villages::where(['id' => $item['villageId']])->value('name');
            }else{
                $item['villageName'] = '所有村';
            }
        }

        $result = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count,
        ];

        return json($result);
    }

}
